package cn.xiuxian.dao.impl;

import cn.xiuxian.dao.IUserDao;
import cn.xiuxian.entity.User;
import cn.xiuxian.util.JdbcTemplate;
import cn.xiuxian.util.JdbcUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@SuppressWarnings("all")
public class UserDaoImpl2 implements IUserDao {
    @Override
    public int save(User user) {
//        return  JdbcTemplate.executeDML("insert into jdbc_user(username,password,power) values(?,?,?)",user.getUsername(),user.getPassword(),user.getPower());
          return  JdbcTemplate.save(user);
    }

    @Override
    public User findByUsername(String username) {
        return null;
    }

    @Override
    public void saveList(List<User> userList) {
        Connection connection = null;
        PreparedStatement  preparedStatement = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            String url = "jdbc:mysql://localhost:3306/dy?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC";
            String user = "root";
            String password = "root";
            connection = DriverManager.getConnection(url,user,password);
            String sql = "insert  into   jdbc_user(username,password,birthday,power) values(?,?,?,?)";
            preparedStatement =connection.prepareStatement(sql);

            for (User u : userList) {
                preparedStatement.setString(1,u.getUsername());
                preparedStatement.setString(2,u.getPassword());

                //  数据日期   -  java.sql.Date
                //  构造 (long time)
                //java.util.Date  父类  => 转换成子类
                preparedStatement.setDate(3,new java.sql.Date(u.getBirthday().getTime()));

                preparedStatement.setInt(4,u.getPower());

                preparedStatement.executeUpdate();
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if(preparedStatement != null){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    @Override
    public void saveBatch(List<User> userList) {
        Connection connection = null;
        PreparedStatement  preparedStatement = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            String url = "jdbc:mysql://localhost:3306/dy?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&rewriteBatchedStatements=true";
            String user = "root";
            String password = "root";
            connection = DriverManager.getConnection(url,user,password);
            String sql = "insert  into   jdbc_user(username,password,birthday,power) values(?,?,?,?)";
            preparedStatement =connection.prepareStatement(sql);

            int count = 0;
            for (User u : userList) {
                preparedStatement.setString(1,u.getUsername());
                preparedStatement.setString(2,u.getPassword());

                //  数据日期   -  java.sql.Date
                //  构造 (long time)
                //java.util.Date  父类  => 转换成子类
                preparedStatement.setDate(3,new java.sql.Date(u.getBirthday().getTime()));

                preparedStatement.setInt(4,u.getPower());

               // preparedStatement.executeUpdate();
                preparedStatement.addBatch();
                if(++count == 10000){
                    preparedStatement.executeBatch();

                    preparedStatement.clearBatch();
                    count = 0;
                }
                if(count != 0){
                    preparedStatement.executeBatch();
                    preparedStatement.clearBatch();
                }
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if(preparedStatement != null){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @Override
    public int deleByid(Integer id) {
//        Connection connection = null;
//        PreparedStatement preparedStatement = null;
//        try {
//            connection = JdbcUtil.getConnection();
//            String sql = "delete from  jdbc_user  where  id = ?";
//            preparedStatement = connection.prepareStatement(sql);
//            preparedStatement.setInt(1,id);
//            preparedStatement.executeUpdate();
//        }catch (Exception e){
//            e.printStackTrace();
//        }finally {
//            JdbcUtil.closeConnection(connection,preparedStatement);
//        }
//        return 0;
//        return JdbcTemplate.executeUpdate(connection -> {
//            String sql = "delete from jdbc_user where id = ?";
//
//            PreparedStatement preparedStatement = connection.prepareStatement(sql);
//            preparedStatement.setInt(1,id);
//
//            return  preparedStatement;
//        });

        return  JdbcTemplate.executeDML("delete  from jdbc_user  where  id = ?",id);
    }

    @Override
    public Object getUserByid(Integer id) {

//        return JdbcTemplate.executeQuery(connection -> {
//            PreparedStatement preparedStatement = connection.prepareStatement("select * from jdbc_user where id = ?");
//            preparedStatement.setInt(1,id);
//            return  preparedStatement;
//        },resultSet -> {
//            User user = null;
//            if (resultSet.next()){
//              String uname = resultSet.getString(2);
//              String pwd = resultSet.getString(3);
//              Date  birthday = resultSet.getDate(4);
//              Integer power = resultSet.getInt(5);
//              user = new User(id,uname,pwd,birthday,power);
//            }
//            return  user;
//        });
        return  JdbcTemplate.queryOne(User.class,"select id,username,password,birthday,power from  jdbc_user  where id = ?",id);
    }

    @Override
    public int deleteByid(User user, Integer id) {
        return JdbcTemplate.deleteByid(user.getClass(),id);
    }

    @Override
    public User getUserByid(User user, Integer id) {
        return JdbcTemplate.getByid(user.getClass(),id);
    }

    @Override
    public List<User> getAllUser(User user) {
        return JdbcTemplate.getAll(user.getClass());
    }

    @Override
    public List<User> getAll(User user) {
        return JdbcTemplate.queryList(user.getClass(),"select *  from  jdbc_user",null);
    }
}
